﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineMaterialsCommand.Commands;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineMaterialsCommand.CommandHandlers
{
    public class AllDeleteMaterialCommandHandler : IRequestHandler<AllDeleteMaterialCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<MaterialsExamine> _materialsRepository;
        public AllDeleteMaterialCommandHandler(IBaseRepository<MaterialsExamine> materialsRepository)
        {
            _materialsRepository = materialsRepository;
        }
        public async Task<APIRequst<int>> Handle(AllDeleteMaterialCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();

            var listto = request.codement.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            if(listto==null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
                return await Task.FromResult(result);
            }

            var listment = _materialsRepository.GetAll().Where(x => listto.Contains(x.MaterialsId)).ToList();
            foreach (var item in listment)
            {
                item.isDel = true;
            }
            var code = await _materialsRepository.UpdateRange(listment);
            if (code > 0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();

            }
            return await Task.FromResult(result);
        }
    }
}
